//
//  OneViewController.m
//  Tongcheng
//
//  Created by   丛熠超 on 2017/4/5.
//  Copyright © 2017年 Tongcheng. All rights reserved.
//

#import "OneViewController.h"
#import "CCPScrollView.h"
#import "NewsModel.h"
#import "PersonalViewController.h"
#import "AppDelegate.h"
#import "MainTableViewCell.h"
#import "MainOperator.h"
#import "GameViewController.h"
#import "MJRefresh.h"

@interface OneViewController ()<UITableViewDelegate, UITableViewDataSource,MainTableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *MaintableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *flowDataArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

static NSString *mainCellIdentifier = @"MainTableViewCell";

@implementation OneViewController

- (void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self.MaintableView reloadData];
}

- (void)setFlowDataArray:(NSMutableArray *)flowDataArray
{
    _flowDataArray = flowDataArray;
    [self setupflowitem];
}

- (MJRefreshAutoNormalFooter *)footer
{
    if (!_footer) {
        _footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(downloadMore)];
    }
    return _footer;
}

- (NSInteger)page
{
    if (!_page) {
        _page = 1;
    }
    return 1;
}

- (void)setupView
{
    _MaintableView.delegate = self;
    _MaintableView.dataSource= self;
    _MaintableView.tableFooterView = [[UIView alloc] init];
    _MaintableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self download];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self download];
    // Do any additional setup after loading the view from its nib.
}

- (void)setupflowitem {
    CCPScrollView *ccpview = [[CCPScrollView alloc] initWithFrame:CGRectMake(0, 24, kScreenWidth, 40)];
    ccpview.titleColor = [UIColor lightGrayColor];
    ccpview.titleFont = 11.0;
    ccpview.backgroundColor = [UIColor colorWithRed:245.0 / 255.0 green:242.0 / 255.0 blue:230.0 / 255.0 alpha:1.0];
    ccpview.titleArray = self.flowDataArray;
    [self.view addSubview:ccpview];
    [ccpview clickTitleLabel:^(NSInteger index, NSString *titleString) {
        GameViewController *gameVc = [[GameViewController alloc] init];
        [self.navigationController pushViewController:gameVc animated:YES];
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
    if (!cell)
    {
        cell = [[UINib nibWithNibName:@"MainTableViewCell" bundle:nil] instantiateWithOwner:self options:nil].lastObject;
    }
    NearByListModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonalViewController *personal = [[PersonalViewController alloc] init];
    NearByListModel *model = self.dataArray[indexPath.row];
    personal.userId = model._id;
    personal.navTitle = model.user_nicename;
    [self.navigationController pushViewController:personal animated:YES];
}

- (void)clickSayHiWith:(NearByListModel *)model withIndex:(NSIndexPath *)indexPath
{
    NSLog(@"say hi");
}

- (void)download
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"page"] = [NSString stringWithFormat:@"%ld", self.page];
    MainOperator *operator = [[MainOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.MaintableView.mj_header endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            self.dataArray = [NSMutableArray arrayWithArray:operator.data.data.list];
            self.flowDataArray = [NSMutableArray arrayWithArray:operator.data.data.redpack];
            if (self.dataArray.count < 30) {
                self.MaintableView.mj_footer = nil;
            }else{
                self.MaintableView.mj_footer = self.footer;
                self.page ++;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.MaintableView.mj_header endRefreshing];
    }];
}

- (void)downloadMore
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"token"] = self.config.token;
    dic[@"user_id"] = self.config.userId;
    dic[@"page"] = [NSString stringWithFormat:@"%ld", self.page];
    MainOperator *operator = [[MainOperator alloc] initWithParams:dic];
    [self.networkingManager asyncTaskOnVC:self operator:operator successBlock:^(BaseModel *baseModel) {
        [self.MaintableView.mj_footer endRefreshing];
        if ([baseModel.code isEqualToString:@"1"]) {
            NSMutableArray *array = [NSMutableArray arrayWithArray:self.dataArray];
            [array addObjectsFromArray:operator.data.data.list];
            self.dataArray = array;
            if (operator.data.data.list.count < 30) {
                self.MaintableView.mj_footer = nil;
            }else{
                self.MaintableView.mj_footer = self.footer;
                self.page ++;
            }
        }
    } failBlock:^(BaseModel *baseModel) {
        [self.MaintableView.mj_footer endRefreshing];
    }];

}

- (void)SayHi {
    NSLog(@"hello");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc {
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
